package game.view.menu
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	
	import game.UIComponent.Button;
	import game.UIComponent.NumberStep;
	import game.UIComponent.Panel;
	import game.common.CustomBitmap;
	import game.controller.GameDispatcher;
	import game.event.EventName;
	import game.event.GameEvent;
	import game.managers.LanguageManager;
	import game.utils.Utils;

	public class SellPanel extends Panel
	{
		private var _foodType:CustomBitmap;
		private var _source:String;
		private var _foodName:TextField;

		private var _textBg:TextField;
		private var _cost:TextField;
		private var _ex:TextField;
		private var _price:TextField;
		private var _purchase:TextField;
		private var _num:TextField;
		private var _pay:TextField;
		private var _payNum:TextField;
		private var _storage:TextField;
		private var _introduce:TextField;
		private var _storage2:TextField;

		private var _buyBtn:Button;

		private var _numStep:NumberStep;
		public function SellPanel()
		{
			init();
		}

		override protected function init():void
		{
			setBgImage("buyPanelBg", 543, 459);
			setCenter();
			close=new Point(483, -10);
			setImageTitle("sellfoodtxt");

			_foodType=new CustomBitmap();
			_foodType.source="yuecai1";
			_foodType.x=30;
			_foodType.y=45;
			_foodType.width=150;
			_foodType.height=150;
			addChild(_foodType);

			_foodName=Utils.getCommonTextField(LanguageManager.getWord("sell.name"), 18,_foodType.x + _foodType.width + 20,_foodType.y);
			_foodName.width=_foodName.textWidth + 20;
			addChild(_foodName);

			_textBg=Utils.getCommonTextField(LanguageManager.getWord("sell.textbg"));
			_textBg.x=_foodName.x;
			_textBg.y=_foodName.y + _foodName.textHeight + 10;
			addChild(_textBg);

			_cost=Utils.getTextField("1");
			_cost.x=_textBg.x + 35;
			_cost.y=_textBg.y;
			addChild(_cost);

			_price=Utils.getTextField("2");
			_price.x=_cost.x + 75;
			_price.y=_cost.y;
			addChild(_price);
			
			_ex=Utils.getTextField("1");
			_ex.x=_price.x + 75;
			_ex.y=_price.y;
			addChild(_ex);
			
			_purchase=Utils.getCommonTextField(LanguageManager.getWord("sell.num"),15,_textBg.x + 3,_textBg.y + 20);
			addChild(_purchase);

//			_numStep=new NumberStep(numChange,(Global.money / int(_cost.text))>1500?1500:Global.money/int(_cost.text),"numstepBg");
			
			_numStep=new NumberStep(numChange,1500,"numstepBg");
			_numStep.value=_numStep.maxNum;
			_numStep.x=_purchase.x + 50;
			_numStep.y=_purchase.y + 25;
			addChild(_numStep);
			
			_num=Utils.getCommonTextField(LanguageManager.getWord("sell.num2"),12,_numStep.x + 50,_numStep.y + _numStep.height);
			addChild(_num);
			
			_pay=Utils.getCommonTextField(LanguageManager.getWord("sell.pay"),12,_num.x - 30,_num.y + _num.textHeight);
			_pay.width = _pay.textWidth;
			addChild(_pay);
			
			_payNum=Utils.getTextField(String(int(_cost.text) * _numStep.value),20);
			_payNum.x=_pay.x + 100;
			_payNum.y=_pay.y;
			addChild(_payNum);
			
			_storage=Utils.getCommonTextField(LanguageManager.getWord("sell.storage"),18,_pay.x,_pay.y + 70);
			addChild(_storage);
			
			_buyBtn=new Button("","miniBuyBtn");
			_buyBtn.x=_storage.x + 35;
			_buyBtn.y=_storage.y + 80;
			_buyBtn.addEventListener(MouseEvent.CLICK,onClick);
			addChild(_buyBtn);
			
			_introduce=Utils.getCommonTextField(LanguageManager.getWord("sell.introduce"),15,_purchase.x,_buyBtn.y + _buyBtn.height + 5);
			addChild(_introduce);
			
			_storage2=Utils.getCommonTextField(LanguageManager.getWord("sell.storage2"),12,_foodType.x-5,this.height - 100);
			addChild(_storage2);
		}
		
		/**
		 * 检查有否与fooddataArr里的数据重复
		 * */
		private function checkFoodDataRepeat(item:*, index:int, arr:Array):Boolean
		{
			if (item == null) 
			{
				arr[index] = obj;
				return true;
			}
			else if (item.source == obj.source) 
			{
				return false;
			}
			return false;
		}
		
		/**
		 * 检查有否与foodArr里的source重复
		 * */
		private function checkFoodSourceRepeat(item:*, index:int, arr:Array):Boolean
		{
			if (item == null) 
			{
				arr[index] = obj.source;
				/**记录是在数组的第几个位置*/
				obj.index = index;
				return true;
			}
			else if (item == obj.source) 
			{
				return false;
			}
			return false;
			
		}
		
		private var obj:Object = new Object();
		
		private function onClick(e:MouseEvent):void
		{		
			obj = new Object();
			if (_numStep.value > 0) 
			{
				Global.money = Global.money - int(_payNum.text);
				obj.ex = int(_ex.text);
				obj.price = int(_price.text);
				obj.source = _source;
				obj.num = _numStep.value;
				Global.foodDataArr.some(checkFoodDataRepeat);
				Global.foodArr.some(checkFoodSourceRepeat);
//				{
//					Global.foodDataArr.push(obj);
//				}				
				GameDispatcher.getInstance().dispatchEvent(new GameEvent(EventName.BUY_SUCCESS,obj));
			}	
			this.isPop = false;
		}
		
		private function numChange(num:int):void
		{
			_payNum.text = String(Math.round(Number(_cost.text) * num));
			checkEnables();
		}
		
		/**
		 * 检测需要支付的金钱是否大于玩家金钱
		 * */
		private function checkEnables():void
		{
			if (int(_payNum.text) > Global.money)
			{
				_buyBtn.enabled = false;
			}
			else
				_buyBtn.enabled = true;
		}
		
		
		public function set foodType(value:CustomBitmap):void
		{
			_foodType=value;
		}

		
		public function set foodName(value:String):void
		{
			_foodName.text=value;
		}

		
		public function set cost(value:Number):void
		{
			_cost.text=value.toString();
			_payNum.text = String(Math.round(value * _numStep.value));
			checkEnables();
		}

		
		public function set ex(value:String):void
		{
			_ex.text=value;
		}

		
		public function set price(value:String):void
		{
			_price.text=value;
		}

		
		public function set introduce(value:String):void
		{
			_introduce.text = value;
		}

		
		public function set maxNum(value:Number):void
		{
			//_numStep.maxNum = value;
			_numStep.value = value;
			//checkEnables();
		}

		
		public function set source(value:String):void
		{
			_source = value;
			_foodType.source = _source;
		}


	}
}